/*
 *  @ProductName@
 *  Copyright (C) @CopyrightYear@
 *    National Institute of Advanced Industrial Science and Technology (AIST)
 *    Registration Number: @AISTRegistrationNumber@
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package jp.go.aist.six.vuln.model.scap.cpe.dictionary;

import jp.go.aist.six.util.orm.AbstractPersistable;
import jp.go.aist.six.vuln.model.scap.cpe.CpeName;
import java.util.ArrayList;
import java.util.Collection;



/**
 * The CpeItem denotes a single CPE Name.
 *
 * @author	Akihito Nakamura, AIST
 * @version $Id: CpeItem.java 168 2010-05-28 10:05:43Z nakamura5akihito $
 */
public class CpeItem
    extends AbstractPersistable
{

    /**
     */
    private String  _name;
    //{cpe_dict:namePattern, required}

    /**
     */
    private Collection<CpeTitle>  _title = new ArrayList<CpeTitle>();
    //{1..*}


    private transient CpeName  _nameStructure;



    /**
     * Constructor.
     */
    public CpeItem()
    {
    }


    /**
     * Constructor.
     */
    public CpeItem(
                    final String name
                    )
    {
        setName( name );
    }



    /**
     */
    public void setName(
                    final String name
                    )
    {
        _name = name;
    }


    /**
     */
    public String getName()
    {
        return _name;
    }



    /**
     */
    public Collection<CpeTitle> getTitle()
    {
        return _title;
    }


    /**
     */
    public void setTitle(
                    final Collection<CpeTitle> title
                    )
    {
        if (title != _title) {
            _title.clear();
            if (title != null) {
                _title.addAll( title );
            }
        }
    }



    /**
     */
    public void setNameStructure(
                    final CpeName name
                    )
    {
        _nameStructure = name;
    }


    /**
     */
    public CpeName getNameStructure()
    {
        if (_nameStructure == null) {
            _nameStructure = new CpeName( getName() );
        }

        return _nameStructure;
    }



    //**************************************************************
    //  java.lang.Object
    //**************************************************************

    /**
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode()
    {
        final int  prime = 37;
        int  result = 17;

        String  name = getName();
        result = prime * result + ((name == null) ? 0 : name.hashCode());

        // NOT related to the equalty!!!
//        Collection<CpeTitle>  title = getTitle();
//        result = prime * result + ((title == null) ? 0 : title.hashCode());

        return result;
    }



    /**
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(
                    final Object obj
                    )
    {
        if (this == obj) {
            return true;
        }

        if (!(obj instanceof CpeItem)) {
            return false;
        }

        CpeItem  other = (CpeItem)obj;
        String  other_name = other.getName();
        String   this_name =  this.getName();
        if (this_name == other_name
                        ||  (this_name != null  &&  this_name.equals( other_name ))) {
            return true;
        }

        return false;
    }



    /**
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString()
    {
        return "CpeItem[name=" + getName()
                + ", title=" + getTitle()
                + "]";
    }

}
// CpeItem
